package xiaopiya.robot.handler.rss.tool;

import java.util.Collection;


/**
 * @author Ray (ayufox@gmail.com)
 * @version 0.1
 */
public class RssContent
{
	private String channel;

	private Collection<ContentItem> items;

	public RssContent(String channel, Collection<ContentItem> items)
	{
		this.channel = channel;
		this.items = items;
	}

	public String getChannel()
	{
		return channel;
	}

	public Collection<ContentItem> getItems()
	{
		return items;
	}
	
	public String toString()
	{
		return buildMessge(this);
	}


	public static String buildMessge(RssContent content)
	{
		if (content == null || content.getItems() == null
				|| content.getItems().size() == 0)
		{
			return null;
		}
		StringBuffer result = new StringBuffer();
		for (ContentItem item : content.getItems())
		{
			if (result.length() > 0)
			{
				result.append("\n");
			}
			result.append(createItemString(item));
		}
		result.insert(0, content.getChannel() + ":\n");
		return result.toString();
	}

	protected static String createItemString(ContentItem item)
	{
		StringBuffer result = new StringBuffer();
		result.append(item.getTitle());
		if (item.getTimestamp() != null)
		{
			result.append("[");
			result.append(item.getTimestamp());
			result.append("]");
		}
		result.append("\n");
		result.append(item.getLink());
		return result.toString();
	}

}
